
Ext.namespace("TR.admin.security");

TR.admin.security.userRole = function(panelId, title, iconClz){

	var userSelectFlag = false;
	var userStore = new Ext.data.JsonStore({
		url : "admin/user/findUserByCon",
		totalProperty : "total",
		root : "rows",
		autoLoad : true,
		fields : ["id", "createDate", "updateDate", "accountExpired", "accountLocked",
			"credentialsExpired", "status", "des", "realName", "username"],
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					status : 1,
					con : userQueryField.getValue()
				});
			}
		}
	});
	var userSm = new Ext.grid.RowSelectionModel({
		singleSelect : true,
		listeners : {
			rowselect : function(){
				var rec = userSm.getSelected();
				ajaxRequest({
					url : "admin/role/listRoleByUser/" + rec.get("id"),
					callback : cb
				});
				//返回需为ExtReturn，带success信息,从而执行callback
				function cb(json){
					userSelectFlag = false;
					var role = $.makeArray(json.data);
					var roleNum = roleStore.getTotalCount();
					var range = [];
					$.each(role, function(n, v){
						for(var i=0; i<roleNum; i++){
							var rec = roleStore.getAt(i);
							if(rec.get("id")===v["id"]){
								range.push(i);
							}
						}
					});
					//选择区域之前将其他已选区域取消选择
					roleSm.selectRows(range);
					userSelectFlag = true;
				}
			}
		}
	});
	var userCm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
		header : "用户名",
		dataIndex : "username"
	}, {
		header : "姓名",
		dataIndex : "realName"
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderStatus
	}, {
		header : "描述",
		dataIndex : "des",
		width : 200
	}]);
	var userQueryField = genQueryField();
	var userQueryBar = ["->", "关键字:", userQueryField, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			userStore.load();
		}
	}];
	var userGrid = new Ext.grid.GridPanel({
		title : "教师",
		store : userStore,
		sm : userSm,
		cm : userCm,
		flex : 5,
		frame : true,
		tbar : genToolBar([], userQueryBar, panelId),
		bbar : genPageBar(userStore)
	});

	var roleStore = new Ext.data.JsonStore({
		url : "admin/role/findRoleByCon",
		totalProperty : "total",
		root : "rows",
		autoLoad : true,
		fields : ["id", "createTime", "updateTime", "des", "roleName", "isRoot", "status"],
		listeners : {
			beforeload : function(s){
				s.removeAll();
				Ext.apply(s.baseParams, {
					status : 1, //必须是启用的role
					con : roleQueryField.getValue()
				});
			}
		}
	});
	var roleSm = new Ext.grid.CheckboxSelectionModel({
		checkOnly : true,
		listeners : {
			rowdeselect : del,
			rowselect : create
		}
	});
	var roleCm = new Ext.grid.ColumnModel([roleSm, new Ext.grid.RowNumberer(), {
		header : "名称",
		dataIndex : "roleName",
		width : 150
	}, {
		header : "状态",
		dataIndex : "status",
		renderer : renderStatus
	}, {
		header : "描述",
		dataIndex : "des",
		width : 200
	}]);
	var roleQueryField = genQueryField();
	var roleQueryBar = [ "->", roleQueryField, {
		text : "查询",
		iconCls : "query",
		handler : function(){
			roleStore.reload();
		}
	}];
	var roleGrid = new Ext.grid.GridPanel({
		title : "关联角色",
		frame : true,
		sm : roleSm,
		cm : roleCm,
		store : roleStore,
		tbar : genToolBar([], roleQueryBar, panelId),
		flex : 5
	});

	return new Ext.Panel({
		id : panelId,
		title : title,
		iconCls : iconClz,
		closable : true,
		layout : "hbox",
		layoutConfig : {
			align : "stretch"
		},
		items : [userGrid, roleGrid]
	});
	function create(sm, rowIndex){
		if(userSelectFlag===false){
			return;
		}
		var userRec = userSm.getSelected();
		if(userRec==null){
			showFailMsg("请选择教师.");
			return;
		}
		ajaxRequest({
			url : "admin/userRole/add",
			params : {
				userId : userRec.get("id"),
				roleId : roleStore.getAt(rowIndex).get("id")
			}
		});
	}
	function del(sm, rowIndex){
		if(userSelectFlag===false){
			return;
		}
		var userRec = userSm.getSelected();
		if(userRec==null){
			showFailMsg("请选择教师.");
			return;
		}
		ajaxRequest({
			url : "admin/userRole/delete",
			params : {
				userId : userRec.get("id"),
				roleId : roleStore.getAt(rowIndex).get("id")
			}
		});
	}

}

registerModule("TR.admin.security.userRole", TR.admin.security.userRole);
